const express = require('express');
const app = express();

// 中间件
app.use(express.json());
app.use(express.urlencoded({ extended: true }));

// 简单路由
app.get('/', (req, res) => {
  res.json({ message: 'Welcome to Node.js Backend' });
});

// 错误处理中间件
app.use((err, req, res, next) => {
  console.error(err.stack);
  res.status(500).send('Something broke!');
});


require('dotenv').config();
const Goods = require('./models/goods.model');

async function main() {
  try {
    // 示例：创建并查询
    // const newGoodsId = await Goods.create('1004', '笔记本电脑', '1', '笔记本电脑222', '1200','金桥阳光花苑');
    // console.log('新用户ID:', newGoodsId);

    const goods = await Goods.findAll();
    // console.log('所有物品:', goods);

    const goodsInfo = await Goods.findById('1001');
    console.log('物品findById:', goodsInfo);
  } catch (err) {
    console.error('应用出错:', err);
  } finally {
    // 如果需要在应用退出时关闭连接池
    const { pool } = require('./config/db.config');
    await pool.end();
  }
}

main();
module.exports = app;